/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import model.Transacoes;
import dao.TransacoesDao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class TransacoesDaoImpl implements TransacoesDao {

    @Override
    public void inserir(Transacoes transacoes) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.save(transacoes);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(transacoes.getVeiculos().getNome(), "Compra aceita!");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(transacoes.getVeiculos().getNome(), "Erro ao comprar");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public void atualizar(Transacoes transacoes) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.saveOrUpdate(transacoes);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(transacoes.getVeiculos().getNome(), "Compra finalizada com sucesso!");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(transacoes.getVeiculos().getNome(), "Erro ao finalizar compra!");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }

    }

    @Override
    public List<Transacoes> selectVendedor(Integer FK_TRANSACOES_VENDEDOR) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Transacoes.class);
            criteria.add(Restrictions.eq("usuariosByFkTransacoesVendedor.pkId", FK_TRANSACOES_VENDEDOR));

            return criteria.list();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public void eliminar(Transacoes transacoes) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {

            session.beginTransaction();
            session.delete(transacoes);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(transacoes.getVeiculos().getNome(), "Compra finalizada com sucesso!");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(transacoes.getVeiculos().getNome(), "Erro ao finalizar compra!");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public List<Transacoes> selectComprador(Integer FK_TRANSACOES_VENDEDOR, Integer pkFkTransacoesVeiculos) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Transacoes.class);
            criteria.add(Restrictions.eq("usuariosByFkTransacoesComprador.pkId", FK_TRANSACOES_VENDEDOR));
            criteria.add(Restrictions.eq("pkFkTransacoesVeiculos", pkFkTransacoesVeiculos));

            return criteria.list();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public Transacoes selectVeiculo(Integer PK_FK_TRANSACOES_VEICULOS) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Transacoes.class);
            criteria.add(Restrictions.eq("pkFkTransacoesVeiculos", PK_FK_TRANSACOES_VEICULOS));

            return (Transacoes) criteria.uniqueResult();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public List<Transacoes> selectComprador(Integer FK_TRANSACOES_COMPRADOR) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            Criteria criteria = session.createCriteria(Transacoes.class);
            criteria.add(Restrictions.eq("usuariosByFkTransacoesComprador.pkId", FK_TRANSACOES_COMPRADOR));

            return criteria.list();

        } catch (HibernateException hibernateException) {
            GrowMessages.errorMessage("Erro", hibernateException.getLocalizedMessage());
            System.out.println(hibernateException.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean isOpen(Integer PK_FK_TRANSACOES_VEICULOS) {
        
        Session session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Transacoes.class);
        criteria.add(Restrictions.eq("pkFkTransacoesVeiculos", PK_FK_TRANSACOES_VEICULOS));
        
        if (criteria.uniqueResult() != null) {
            return true;
        } else {
            return false;
        }
    }
}
